package com.luxoft.scoremanager.server.dao.hibernateimpl;

import com.luxoft.scoremanager.server.dao.StaticDataDao;
import com.luxoft.scoremanager.server.dao.exception.HibernateRecordException;
import com.luxoft.scoremanager.server.dao.hibernateimpl.util.HibernateUtil;
import com.luxoft.scoremanager.server.model.dao.StaticDataDaoModel;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Александр on 24.09.13.
 */
public class HibernateStaticDataDao implements StaticDataDao {
    @Autowired
    private static Logger log = Logger.getLogger(HibernateStaticDataDao.class);

    @Override
    public void addStaticData(StaticDataDaoModel staticData) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(staticData);
            session.getTransaction().commit();
        } catch (Exception e) {
            log.info("Error component add");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void updateStaticData(StaticDataDaoModel staticData) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(staticData);
            session.getTransaction().commit();
        } catch (Exception e) {
            log.info("Error score package update");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void deleteStaticData(StaticDataDaoModel staticData) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(staticData);
            session.getTransaction().commit();
        } catch (Exception e) {
            log.info("Error component delete");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public List<StaticDataDaoModel> getAll() {
        Session session = null;
        List staticDataList = new ArrayList<StaticDataDaoModel>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            staticDataList = session.createCriteria(StaticDataDaoModel.class).list();
        } catch (Exception e) {
            log.info("Error component get all");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return staticDataList;
    }

    @Override
    public List<StaticDataDaoModel> getAllBySet(String set) {
        Session session = null;
        List staticDataList = new ArrayList<StaticDataDaoModel>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            String hql = "FROM StaticDataDaoModel E WHERE E.staticDataSet = :set";
            Query query = session.createQuery(hql);
            query.setParameter("set", set);
            staticDataList = query.list();
        } catch (Exception e) {
            log.info("Error component get all");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return staticDataList;
    }

    @Override
    public StaticDataDaoModel getById(int id) {
        Session session = null;
        StaticDataDaoModel staticData = new StaticDataDaoModel();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            staticData = (StaticDataDaoModel) session.get(StaticDataDaoModel.class, id);
        } catch (Exception e) {
            log.info("Error component get by id");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return staticData;
    }
}